import $C from '@/common/config.js';
import $loginInfo from '@/common/login-info.js';
import $store from '@/store'

var isjump = false;
export default {
	common:{
		method: 'GET',
		data:{}
	},
	getHeader() {
		let userToken =  uni.getStorageSync('token') || '';//740f45b7-c0e9-4db0-8e5a-8341ab0fc034
		return {
			"content-type":"application/json",
			"token": userToken,
		}
	},
	
	
	// get请求
	get(url,data = {},options = {}){
		options.url = url
		options.data = data
		options.method = "GET"
		options.headerContentType = "application/x-www-form-urlencoded"
		return this.request(options)
	},
	post(url,data = {},options = {}){
		options.url = url
		options.data = data
		options.method = "POST"
		options.headerContentType = "application/x-www-form-urlencoded"
		return this.request(options)
	},
	postRaw(url,data = {},options = {}){
		options.url = url
		options.data = JSON.stringify(data)
		options.method = "POST"
		options.headerContentType = "application/json"
		return this.request(options)
	},
	request(options = {}){
		let that = this;
		options.url = $C.Host + options.url
		options.method = options.method || this.common.method
		// options.dataType = "string"
		let header = this.getHeader()
		if(options.headerContentType) {
			let contentTypeKey = "content-type"
			header[contentTypeKey] = options.headerContentType
		}
		options.header = options.header || header
		options.withCredentials = true
		return new Promise((res,rej)=>{
			uni.request({
				...options,
				success:(result) => {
					// 请求成功
					if(result.data.code == 1){
						// 接口正常访问 返回请求数据
						res(result.data.data)
					}else if(result.data.code == 401){
						$store.commit('userToken','')
						$store.commit('userInfo','')
						$store.commit('userMobile','')
						uni.removeStorageSync('userToken');
						uni.removeStorageSync('userInfo');
						uni.removeStorageSync('userMobile');
						uni.showToast({
							title:'登录已失效，请先登录',
							icon:'none'
						})
						// #ifdef H5
							location.replace('/index/index/wechat_login.html?return_url='+'/course/')
						// #endif
						if(!$C.jumplogin){
							$C.jumplogin = true;
							setTimeout(function(){
								uni.switchTab({
									url:'/pages/index/index'
								})
								uni.$emit('openLogin')
								$C.jumplogin = false;
							},2000)
						}
						return rej(result.data);
					}else if(result.data.code == 3000){
						// 未绑定手机号
						uni.showToast({
							title:result.data.msg,
							icon:'none'
						})
						if(!$C.jumplogin){
							$C.jumplogin = true;
							setTimeout(function(){
								uni.switchTab({
									url:'/pages/index/index'
								})
								uni.$emit('openLogin')
								$C.jumplogin = false;
							},2000)
						}
						return rej(result.data);
					}else{
						uni.showToast({
							title:result.data.msg || '请求失败',
							icon:'none'
						})
						return rej(result.data);
					}
				},
				fail: (error) => {
					// 断网超时情况
					uni.showToast({
						title:error.errMsg || '请求失败',
						icon:'none'
					})
					if(rej){
						return rej();
					}
				}
			})
		})
		
		// return uni.request(options);
	},
	
	upload(tempfile,data = {}) {
		let header = {"token":uni.getStorageSync('userToken') || ''}//17682cbb-6ebe-4c54-b506-c42240bd44ce
		return new Promise((res,rej)=>{
			uni.uploadFile({
				url: $C.Host + 'api/common/upload', //仅为示例，非真实的接口地址
				filePath: tempfile,	//res.tempFilePaths[0]
				name: 'file',
				header:header,
				formData: data,
				success:(result) => {
					if(result.data && typeof(result.data) == 'string') {
						result.data = JSON.parse(result.data)
					}
					if(result.statusCode !== 200){
						uni.showToast({
							title:result.data.msg || '请求失败',
							icon:'none'
						})
						return rej(result.data);
					}
					// 请求成功
					if(result.data.code == 1){
						// 接口正常访问 返回请求数据
						res(result.data.data)
					}
					else if(result.data.code == 1000){
						uni.showToast({
							title:result.data.msg,
							icon:'none'
						})
						if(!$C.jumplogin){
							$C.jumplogin = true;
							setTimeout(function(){
								uni.switchTab({
									url:'/pages/index/index'
								})
								uni.$emit('openLogin')
								$C.jumplogin = false;
							},2000)
						}
						return rej(result.data);
					}else if(result.data.code == 3000){
						// 未绑定手机号
						uni.showToast({
							title:result.data.msg,
							icon:'none'
						})
						if(!$C.jumplogin){
							$C.jumplogin = true;
							setTimeout(function(){
								uni.switchTab({
									url:'/pages/index/index'
								})
								uni.$emit('openLogin')
								$C.jumplogin = false;
							},2000)
						}
						return rej(result.data);
					}else{
						uni.showToast({
							title:result.data.msg || '请求失败',
							icon:'none'
						})
						return rej(result.data);
					}
				},
				fail: (error) => {
					console.log(33333)
					// 断网超时情况
					uni.showToast({
						title:error.errMsg || '请求失败',
						icon:'none'
					})
					return rej();
				}
			});
		})
		
	},
	// 微信支付
	wxPay(options = {}) {
		console.log('wxPaywxPaywxPay')
		let {NonceStr,Package,PaySign,SignType,TimeStamp,url,type} = options;
	
		uni.requestOrderPayment({
			provider: 'wxpay',
			nonceStr: NonceStr,
			package: Package,
			paySign:PaySign,
			signType: SignType,
			timeStamp: TimeStamp,
			success: function(res) {
				
				 setTimeout(() => {
					 if(type == 'redirect'){
						 uni.redirectTo({
							url:url
						 })
					 }else if(type == 'reLaunch'){
						 uni.reLaunch({
						 	url:url
						 })
					 }else if(type == 'switchtab'){
						 uni.switchTab({
						 	url:url
						 })
					 }else{
						uni.navigateTo({
							url:url
						}) 
					 }
					
				}, 800) 
			},
			fail: function(err) {
				
				
				uni.reLaunch({
					url:url
				})
				
				return
				//_this.$api.msg('已取消支付')
				// setTimeout(() => {
				// 	uni.navigateBack()
				// }, 800)
			}
		});
	},
}